//获取应用实例
var util = require('../../utils/phoneutils.js')
var util_carnum = require('../../utils/isLicenseNo.js')
var Util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp(),that;
Page({
  data: {
    /**
     * 页面配置
     */
       w:0,
    result:[],
    autoLoadedDriverId:"",
    winWidth: 0,
    winHeight: 0,
    // tab切换 
    currentTab: 0,
    //toast默认不显示 
    isShowToast: false,
    hidden: false,
    nocancel: false,
    showModal: false,
    phone:"",
    phoneNum:'',
    _phoneNumhint:"请输入随车手机号",
    _cph:"请输入车牌号",
    mobile: "",
    appkey: "",
    _Name:"请输入真实姓名",
    carNum:'',
    Name:'',
    hasMore: false,
    hasRefesh: false,
    pageSize:5,
    page:1,
    windowHeight:0,
    windowWidth:0


  },
  /**
    * 弹窗
    */
  showDialogBtn: function () {
    this.setData({
      showModal: true
    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  showToast: function () {
    var _this = this;
    // toast时间 
    _this.data.count = parseInt(_this.data.count) ? parseInt(_this.data.count) : 3000;
    // 显示toast 
    _this.setData({
      isShowToast: true,
    });
    // 定时器关闭 
    setTimeout(function () {
      _this.setData({
        isShowToast: false
      });
    }, _this.data.count);
  },
 
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.setData({
      phoneNum: '',
      carNum: '',
      Name: '',
      _phoneNumhint: "请输入随车手机号",
      _cph: "请输入车牌号",
      _Name: "请输入真实姓名",
    });
    this.setData({
      autoLoadedDriverId: "0"
    })
    this.hideModal();
   
   
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    this.setData({
      autoLoadedDriverId: "1"
    })
    this.hideModal();
   
  },
  onShow:function(e){
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      }
    })
  },
  pullDownRefresh: function (e) {
    console.log("下拉刷新....")
    this.onLoad()
    wx.showToast({
          title: '刷新成功',
          icon: 'success',
          duration: 2000
    })

  },

  pullUpLoad: function (e) {
    this.setData({
      page: this.data.page + 1
    })
    wx.showToast({
      title: '加载中.....',
      icon: "loading",
      duration: 2000
    })
    console.log("--手机号：----" + that.mobile);
    console.log("---验证---" + that.appkey);
    var userinfos = wx.getStorageSync('userinfos');
    var mobile = userinfos.mobile;   
    var myDate = new Date();

    var thistime = myDate.getTime();

    var appkey = app.globalData.appkey;

    var appsecret = app.globalData.appsecret;


    var str = mobile + appsecret + thistime;

    console.log('----------------' + str)


    var appsign = utilMd5.md5(str);

    appsign = appsign.replace("+", "%2B");

    appsign = appsign.replace("=", "%2A");


    console.log(appsign + '-----------------------------appsign')
    wx.request({
      url: getApp().globalData.url,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },

      data: Util.json2Form({
        mobile: that.data.mobile,
        appkey: appkey,
        viewtype: 2,
        pageSize:10,
        page: that.data.page,
        appsign: appsign,
        date: thistime,

      }),
      success: function (res) {
        console.log(res.data);
        var r = JSON.parse(res.data.errorMsg);
        console.log("-----" + r);
        if (res.data.success) {
          that.setData({
            result: that.data.result.concat(r),

            page: that.data.page,

          });
          wx.hideLoading();
        }
       


        //初始化的时候渲染wxSearchdata
        console.log(res.data.errorMsg);
        // var  result =res.data;

        console.log("----" + that.data.result);
        for (var i = 0; i < that.data.result.length; i++) {
          console.log("===所有司机=" + that.data.result[i].nickname);

        }
      }
    })
    console.log("上拉拉加载更多...." + this.data.page)

    //this.getDataFromServer(this.data.page)
  },
  onLoad: function () {
    app.editTabBar();
    //添加tabBar数据  
    console.log('onLoad')
    wx.setNavigationBarTitle({
      title: '我的车队',
    })
    that = this
    var userinfos = wx.getStorageSync('userinfos');
    var mobile = userinfos.mobile;   
//     var appkey = userinfos.appkey;
    that.mobile=mobile;
    that.appkey = appkey;
   that. setData({
      mobile: mobile,
      // appkey: appkey
    })

  

  
   console.log(that.data.page)
   console.log("--手机号：----" + that.mobile);
   console.log("---验证---" + that.appkey);
   console.log('我开始加载了')
   console.log(that.data.page);
   var myDate = new Date();

   var thistime = myDate.getTime();

   var appkey = app.globalData.appkey;
   console.log(appkey+'===============================')
   var appsecret = app.globalData.appsecret;


   var str = mobile + appsecret + thistime;

   console.log('----------------' + str)


   var appsign = utilMd5.md5(str);

   appsign = appsign.replace("+", "%2B");

   appsign = appsign.replace("=", "%2A");


 
   this.setData({
         page:1
   })
   console.log(appsign + '-----------------------------appsign')
   console.log(mobile + '-----------------------------mobile')
   wx.request({
         url: getApp().globalData.url,
         method: 'POST',
         header: {
               "Content-Type": "application/x-www-form-urlencoded"
         },

         data: Util.json2Form({
               mobile: that.data.mobile,
               appkey: appkey,
               viewtype: 2,
               pageSize: 20,
               page: that.data.page,
               appsign: appsign,
               date: thistime,

         }),
         success: function (res) {
               console.log(res.data);
               var r = JSON.parse(res.data.errorMsg);
               console.log("-----" + r);
               if (res.data.success) {
                     that.setData({
                           result: r,
                           page: that.data.page,

                     });
                     wx.hideLoading();
               }



               //初始化的时候渲染wxSearchdata
               console.log(res.data.errorMsg);
               // var  result =res.data;

               console.log("----" + that.data.result);
               for (var i = 0; i < that.data.result.length; i++) {
                     console.log("===所有司机=" + that.data.result[i].nickname);

               }
         }
   })
    //调用应用实例的方法获取全局数据
    /**
* 获取系统信息
*/
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },  /**
       * 滑动切换tab
       */
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });

   
  },
  /*
  检查司机是否存在，通过手机号码判断是否存在
  **/
  checkDriveisEixt:function(mobile,appkey,phone){
    wx.showToast({
      title: '加载中......',
      icon: 'loading',
      duration: 2000
    })
    
    console.log("--手机号：----" + mobile);
    console.log("---验证---" + appkey);
    console.log('===phone===='+phone)
    var myDate = new Date();

    var thistime = myDate.getTime();

    var appkey = app.globalData.appkey;

    var appsecret = app.globalData.appsecret;


    var str = mobile + appsecret + thistime;

    console.log('----------------' + str)


    var appsign = utilMd5.md5(str);

    appsign = appsign.replace("+", "%2B");

    appsign = appsign.replace("=", "%2A");


    console.log(appsign + '-----------------------------appsign')

    wx.request({
      url: getApp().globalData.url,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      /*
      {success: false, errorCode: "0", errorMsg: "该手机号不存在！"}
      */
      data: Util.json2Form({
        mobile: mobile,
        appkey: appkey,
        viewtype: 6,
        
        phone:phone,
        appsign: appsign,
        date: thistime,
      }),
      success: function (res) {
        wx.hideLoading();
        console.log(res.data);
        var a=res.data;
        var r = JSON.parse(res.data.errorMsg);
        console.log("-----" + r);
        that.setData({
          res: r
        });
        if(a.success){
          that.setData({
         showModal: true,

    })
        }
        
        // //初始化的时候渲染wxSearchdata
        // console.log(res.data.errorMsg);
        // var  result =res.data;


      }
    })
     
  },
  leaveinput:function(e){
    console.log("----leaveinput---手机号码：" + e.detail.value);
    if (!e.detail.value){  
      this.setData({
        count: 1500,
        toastText: '手机号码不能为空'

 })
      this.showToast();  
      return;   
     
    }
    if (e.detail.value) {
      var p = util.checkMobile(e.detail.value);
      if (!p) {
        this.setData({
          count: 1500,
          toastText: '手机号码格式不对！'
        })
        this.showToast();
        return;
      }
      else{
            console.log('=====================' + that.mobile)
        that.checkDriveisEixt(that.mobile, that.appkey, e.detail.value);
      }
    }
   
    
  },
  lister_sjh:function(e){
    console.log("手机号码："+e.detail.value);
    if(e.detail.value!=""){
      this.setData({
        phone: e.detail.value,

      })
    }
   
    
  },
  lister_cph:function(e){
    console.log("车牌号：" + e.detail.value);
    this.setData({
      carNum: e.detail.value,

    })
   
  },
  lister_name:function(e){
    console.log("姓名：" + e.detail.value);
    this.setData({
      Name: e.detail.value,

    })
    
  },
  //加载更多
  loadMore: function (e) {
    var that = this;
    that.setData({
      hasRefesh: true,
    });
    page++;
    that.setData({
      page:page
    });
    wx.showToast({
      title: '加载中.....',
      icon: "loading",
      duration: 2000
    })
    console.log("--手机号：----" + that.mobile);
    console.log("---验证---" + that.appkey);
    var myDate = new Date();

    var thistime = myDate.getTime();

    var appkey = app.globalData.appkey;

    var appsecret = app.globalData.appsecret;


    var str = mobile + appsecret + thistime;

    console.log('----------------' + str)


    var appsign = utilMd5.md5(str);

    appsign = appsign.replace("+", "%2B");

    appsign = appsign.replace("=", "%2A");


    console.log(appsign + '-----------------------------appsign')
    wx.request({
          url: getApp().globalData.url,
          method: 'POST',
          header: {
                "Content-Type": "application/x-www-form-urlencoded"
          },

          data: Util.json2Form({
                mobile: that.mobile,
                appkey: appkey,
                viewtype: 2,
                pageSize: 20,
                page: that.data.page,
                appsign: appsign,
                date: thistime,

          }),
          success: function (res) {
                console.log(res.data);
                var r = JSON.parse(res.data.errorMsg);
                console.log("-----" + r);
                if (res.data.success) {
                      wx.hideLoading();
                }
                that.setData({
                      result: r
                });
                //初始化的时候渲染wxSearchdata
                console.log(res.data.errorMsg);
                // var  result =res.data;

                console.log("----" + that.data.result);
                for (var i = 0; i < that.data.result.length; i++) {
                      console.log("===所有司机=" + that.data.result[i].nickname);

                }
          }
    })
   


    
   
  },
  //刷新处理
  refesh: function (e) {
    var that = this;
    that.setData({
      hasRefesh: true,
    });
    var that = this;
    that.setData({
      hasRefesh: true,
    });
     that.getDriveNames();
    that.setData({
      list: res.data.result.list,
      hidden: true,
      page: 1,
      hasRefesh: false,
    });

  },
  
 
  
  getDriveNames: function () {
        var userinfos = wx.getStorageSync('userinfos');
        var mobile = userinfos.mobile;
    wx.showToast({
      title: '加载中.....',
      icon:"loading",
     
    })
    console.log("--手机号：----" + that.mobile);
    console.log("---验证---" + that.appkey);
    console.log('我开始加载了')
    console.log(that.data.page+'------------------------------我是page')
    var myDate = new Date();

    var thistime = myDate.getTime();

    var appkey = app.globalData.appkey;

    var appsecret = app.globalData.appsecret;


    var str = mobile + appsecret + thistime;

    console.log('----------------' + str)


    var appsign = utilMd5.md5(str);

    appsign = appsign.replace("+", "%2B");

    appsign = appsign.replace("=", "%2A");


    console.log(appsign + '-----------------------------appsign')
    wx.request({
      url: getApp().globalData.url,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },

      data: Util.json2Form({
        mobile:mobile,
        appkey: appkey,
        viewtype: 2,
        pageSize: 20,
        page: that.data.page,
        appsign: appsign,
        date: thistime,
      }),
      success: function (res) {
        console.log(res.data);
        var r = JSON.parse(res.data.errorMsg);
        console.log("-----" + r);
         if(res.data.success){
           wx.hideLoading();
         }
        that.setData({
          result: r
        });
        //初始化的时候渲染wxSearchdata
      
        // var  result =res.data;
         
        console.log("----" + that.data.result);
        for (var i = 0; i < that.data.result.length; i++) {
          console.log("===所有司机=" + that.data.result[i].nickname);

        }
      }
    })

  },
  /**
   * 点击tab切换
   */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab==0){
      that.getDriveNames();
    }
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  addCarinfo: function (mobile, appkey, phone, driverName, carNumber,autoLoadedDriverId){
    wx.showToast({
      title: '提交中......',
      icon: 'loading',
     
    })
     console.log("--手机号：----" + mobile);
    console.log("---验证---" + appkey);
    console.log("-----是否复用--" + autoLoadedDriverId);
    var myDate = new Date();

    var thistime = myDate.getTime();

    var appkey = app.globalData.appkey;

    var appsecret = app.globalData.appsecret;


    var str = mobile + appsecret + thistime;

    console.log('----------------' + str)


    var appsign = utilMd5.md5(str);

    appsign = appsign.replace("+", "%2B");

    appsign = appsign.replace("=", "%2A");


    console.log(appsign + '-----------------------------appsign')
   wx.request({
         url: getApp().globalData.url,
         method: 'POST',
         header: {
               "Content-Type": "application/x-www-form-urlencoded"
         },
         data: Util.json2Form({
               mobile: mobile,
               appkey: appkey,
               viewtype: 5,
               phone: phone,
               driverName: driverName,
               carNumber: carNumber,
               autoLoadedDriverId: autoLoadedDriverId,
               appsign: appsign,
               date: thistime,

         }),
         success:  res => {
               console.log(res.data);
               if (res.data.success) {
                     wx.hideLoading();
                  //    wx.showToast({
                  //          title: res.data.errorMsg,
                  //          icon: 'success',
                  //          duration: 2000
                  //    })
                    this.setData({
                           count: 1500,
                           toastText: res.data.errorMsg
                     });
                    this.showToast();  
               }
         }
   });
     
//         // var r = JSON.parse(res.data.errorMsg);
//         // console.log("-----" + r);

//         // that.setData({
//         //   result: r
//         // });
//         // //初始化的时候渲染wxSearchdata
//         // console.log(res.data.errorMsg);
//         // // var  result =res.data;

//         // console.log("----" + that.data.result);
//         // for (var i = 0; i < that.data.result.length; i++) {
//         //   console.log("===所有司机=" + that.data.result[i].nickname);

//         // }
//       }
//     })
  },
  sendinfo:function(){
    console.log("22333"+that.data.phone);
   var p= util.checkMobile(this.data.phone);
   var car_num = util_carnum.isLicenseNo(this.data.carNum);
   console.log(p);
//     if (!this.data.phoneNum){  
//       this.setData({
//         count: 1500,
//         toastText: '手机号码不能为空'
        
//  })
//       this.showToast();  
//       return;   
//       // wx.showToast({
//       //   title: '手机号码不能为空',
//       //   icon: 'succes',
//       //   duration: 1000,
//       //   mask: true
//       // })
//     }
    if(this.data.phoneNum!=""){
      if (!p) {
        this.setData({
          count: 1500,
          toastText: '手机号码格式不对！'
        })
        this.showToast();
      }
      return;
    }
    if (this.data.carNum=="") {
      this.setData({
        count: 1500,
        toastText: '车牌号码不能为空'
      })
      this.showToast(); 
      return;
    }
    // if(this.data.carNum){
    //   if (!car_num) {
    //     this.setData({
    //       count: 1500,
    //       toastText: '车牌号码格式不对！'
    //     })
    //     this.showToast();
    //     return;
    //   }
    // }
    if (this.data.Name=="") {
      this.setData({
        count: 1500,
        toastText: '姓名不能为空'
      })
      this.showToast(); 
      return;
    }
    that.addCarinfo(that.data.mobile, that.data.appkey, that.data.phone, that.data.Name, that.data.carNum, that.data.autoLoadedDriverId);
    if (this.data.phoneNum && this.data.Name && this.data.carNum){
      // this.setData({
      //   showModal: true,
      // (mobile, appkey, phone, driverName, carNumber, autoLoadedDriverId)
      // })

    }
    

    
  },
//   发送短信
driver:function(res){
      var userinfos = wx.getStorageSync('userinfos');
      var mobile = userinfos.mobile;
      console.log(res.target.dataset.did+'--------司机id')   
      this.setData({
            btnid: res.currentTarget.id,
           
      });
      var myDate = new Date();

      var thistime = myDate.getTime();

      var appkey = app.globalData.appkey;

      var appsecret = app.globalData.appsecret;


      var str = mobile + appsecret + thistime;

      console.log('----------------' + str)


      var appsign = utilMd5.md5(str);

      appsign = appsign.replace("+", "%2B");

      appsign = appsign.replace("=", "%2A");


      console.log(appsign + '-----------------------------appsign')
      wx.request({
            url: getApp().globalData.url,
            method: 'POST',
            header: {
                  "Content-Type": "application/x-www-form-urlencoded"
            },
            data: Util.json2Form({
                  mobile:mobile,
                  appkey: appkey,
                  viewtype: 8,
                  driverid:res.target.dataset.did,
                  appsign: appsign,
                  date: thistime,
            }),
            success: res =>{
                  console.log(res);
               
                  this.setData({
                        count: 1500,
                        toastText: res.data.errorMsg
                  });
                  this.showToast();  
              
            }
      });

},
getsdd: function (res) {
      console.log(res);
      this.setData({
            dd: res.detail.value,
            ddlen: res.detail.cursor
      })
      console.log('--------------------------' + this.data.dd);
      var userinfos = wx.getStorageSync('userinfos');
      var mobile = userinfos.mobile;
      var appkey = userinfos.appkey;
      if (this.data.ddlen != 0) {

            console.log(this.data.dd + "dddddddddddddddddddddddd这是我的搜索内容");
            console.log(that.data.page)
            var myDate = new Date();

            var thistime = myDate.getTime();

            var appkey = app.globalData.appkey;

            var appsecret = app.globalData.appsecret;


            var str = mobile + appsecret + thistime;

            console.log('----------------' + str)


            var appsign = utilMd5.md5(str);

            appsign = appsign.replace("+", "%2B");

            appsign = appsign.replace("=", "%2A");


            console.log(appsign + '-----------------------------appsign')
            // 发送请求
            wx.request({
                  url: getApp().globalData.url,
                  method: 'POST',
                  header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                  },
                  data: Util.json2Form({
                        mobile: mobile,
                        appkey: appkey,
                        viewtype: 2,
                        pageSize: 20,
                        search: this.data.dd,
                        page:1,
                        appsign: appsign,
                        date: thistime,

                  }),
                  success: res => {
                        console.log(res);
                        var r = JSON.parse(res.data.errorMsg);
                        console.log("-----" + r);

                        this.setData({
                              result: r,
                        });
                        console.log(this.data.result)
                  }
            });
      }
},

})

